package com.kgc.scd.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.scd.bean.SensitiveType;
import com.kgc.scd.bean.SensitiveWord;
import com.kgc.scd.bean.SensitiveWordResult;
import com.kgc.scd.mapper.SensitiveWordMapper;
import com.kgc.scd.mapper.SensitiveWordTypeMapper;
import com.kgc.scd.service.SensitiveWordService;
import com.kgc.scd.util.PageSupport;
import com.kgc.scd.util.RedisKeyUtils;
import com.kgc.scd.util.RedisUtils;
import com.kgc.scd.util.SensitiveWordCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created On : 2022/6/14.
 * <p>
 * Author : xzy
 * <p>
 * Description: SensitiveWordServiceImpl
 */
@Service
public class SensitiveWordServiceImpl implements SensitiveWordService {

    @Autowired(required = false)
    private SensitiveWordMapper sensitiveWordMapper;

    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private SensitiveWordCache sensitiveWordCache;
    @Autowired(required = false)
    private SensitiveWordTypeMapper sensitiveWordTypeMapper;

    @Override
    public List<SensitiveWord> getSensitiveWords() {
        return sensitiveWordMapper.selectList(null);
    }

    @Override
    public PageSupport<SensitiveWordResult> getAllSensitiveWord(int pageNo, int pageSize) {
        PageHelper.startPage(pageNo,pageSize);
        List<SensitiveWordResult> sensitiveWordResults = sensitiveWordMapper.selectAllSensitiveWord();

        PageInfo<SensitiveWordResult> pageInfo = new PageInfo<>(sensitiveWordResults);
        PageSupport<SensitiveWordResult> sensitiveWordResultPageSupport = new PageSupport<>();
        sensitiveWordResultPageSupport.setPageNo(pageNo);
        sensitiveWordResultPageSupport.setPageSize(pageSize);
        sensitiveWordResultPageSupport.setTotalPage(pageInfo.getPages());
        sensitiveWordResultPageSupport.setTotalCount((int)pageInfo.getTotal());
        sensitiveWordResultPageSupport.setPageData(sensitiveWordResults);
        return sensitiveWordResultPageSupport;
    }

    @Override
    public boolean deleteSensitiveWord(String sensitiveWord) {
        QueryWrapper<SensitiveWord> wrapper = new QueryWrapper<>();
        wrapper.eq("word",sensitiveWord);
        boolean b = sensitiveWordMapper.delete(wrapper) > 0;
        redisUtils.del(RedisKeyUtils.getSensitiveWordKey(SensitiveWordCache.class.getName()));
        sensitiveWordCache.initSensitiveWord();
        return b;
    }

    @Override
    public boolean addSensitiveWord(String categoryId,String sensitiveWord) {
        SensitiveWord sensitiveWord1 = new SensitiveWord();
        sensitiveWord1.setTid(categoryId);
        sensitiveWord1.setWord(sensitiveWord);
        boolean b = sensitiveWordMapper.insert(sensitiveWord1) > 0;
        redisUtils.del(RedisKeyUtils.getSensitiveWordKey(SensitiveWordCache.class.getName()));
        sensitiveWordCache.initSensitiveWord();
        return b;
    }

    @Override
    public List<SensitiveType> getSensitiveWordType() {
        List<SensitiveType> sensitiveTypes = sensitiveWordTypeMapper.selectList(null);
        return sensitiveTypes;
    }
}
